package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.boot.starter.lock.annotation.JRepeat;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.job.rms.entity.RmsCourse;
import org.jeecg.modules.job.rms.entity.RmsCourseApply;
import org.jeecg.modules.job.rms.service.IRmsCourseApplyService;
import org.jeecg.modules.job.rms.service.IRmsCourseService;
import org.jeecg.modules.job.ums.entity.UmsAccount;
import org.jeecg.modules.job.ums.entity.UmsAccountRecords;
import org.jeecg.modules.job.ums.entity.UmsWithdraw;
import org.jeecg.modules.job.ums.service.IUmsAccountRecordsService;
import org.jeecg.modules.job.ums.service.IUmsAccountService;
import org.jeecg.modules.job.ums.service.IUmsWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 财务管理模块
 */
@RestController
@RequestMapping("/api/finance")
@Api(tags="移动端课财务管理模块")
@Slf4j
public class FinanceController {
    @Autowired
    private IUmsAccountRecordsService accountRecordsService;
    @Autowired
    private IUmsAccountService accountService;
    @Autowired
    private IUmsWithdrawService withdrawService;


    /**
     * 查询收支明细
     */
    @ResponseBody
    @RequestMapping(value = "/getFinanceList", method = RequestMethod.GET)
    @ApiOperation(value = "查询收支明细", notes = "查询收支明细")
    public Result<Object> getApplyList(UmsAccountRecords param,@RequestParam(name="page", defaultValue="1") Integer page,
                                       @RequestParam(name="limit", defaultValue="10") Integer limit) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        param.setMemberId(user.getId());
        IPage<UmsAccountRecords> pageInfo = accountRecordsService.page(new Page<>(page,limit),new QueryWrapper<>(param));
        return Result.OK(pageInfo);
    }


    /**
     * 查询提现记录
     */
    @ResponseBody
    @RequestMapping(value = "/getWithdrawList", method = RequestMethod.GET)
    @ApiOperation(value = "查询提现记录", notes = "查询提现记录")
    public Result<Object> getWithdrawList(UmsWithdraw param,
                                          @RequestParam(name="page", defaultValue="1") Integer page,
                                          @RequestParam(name="limit", defaultValue="10") Integer limit) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        param.setMemberId(user.getId());
        IPage<UmsWithdraw> pageInfo = withdrawService.page(new Page<>(page,limit),new QueryWrapper<>(param));
        return Result.OK(pageInfo);
    }

    /**
     * 查询账户余额
     */
    @ResponseBody
    @RequestMapping(value = "/getAccountDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查询账户余额", notes = "查询账户余额")
    public Result<Object> getApplyDetail() {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            UmsAccount result = accountService.getOne(new LambdaQueryWrapper<UmsAccount>().eq(UmsAccount::getMemberId,user.getId()));
            if (result==null){
                result=new UmsAccount();
                //创建会员账户信息
                result.setMemberId(user.getId());
                result.setBalance(BigDecimal.ZERO);
                result.setBalanceWithdraw(BigDecimal.ZERO);
                result.setBalanceFrozen(BigDecimal.ZERO);
                accountService.save(result);
            }
            //查询本月收入
            String month= DateUtils.getCurrentTime("yyyy-MM");
            UmsAccountRecords accountRecordsResult=accountRecordsService.getStatistics(new UmsAccountRecords().setMemberId(user.getId()).setStartDate(month));
            if (accountRecordsResult!=null){
                result.setTotalMoney(accountRecordsResult.getMoney());
            }else{
                result.setTotalMoney(BigDecimal.ZERO);
            }
            return Result.OK(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 提现申请
     */
    @JRepeat(lockKey = "withdrawApply", lockTime = 5)
    @ResponseBody
    @RequestMapping(value = "/withdrawApply", method = RequestMethod.POST)
    @ApiOperation(value = "提现申请", notes = "提现申请")
    public Result<Object> withdrawApply(@RequestBody UmsWithdraw param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            withdrawService.add(param);
            return Result.ok();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}



